package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.query.CustomerChartQO;
import cn.wolfcode.crm.query.CustomerQueryObject;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.vo.CustomerChartVO;
import com.alibaba.druid.support.json.JSONUtils;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerChart")
public class CustomerChartController {
    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/table")
    public String list(Model model, CustomerChartQO qo) {
        List<CustomerChartVO> datas = customerService.queryCustomerChart(qo);
        model.addAttribute("datas", datas);
        model.addAttribute("qo", qo);
        return "chart/customer_table";
    }

    @RequestMapping("/bar")
    public String bar(Model model, CustomerChartQO qo) {
        List<CustomerChartVO> datas = customerService.queryCustomerChart(qo);
        List<String> groups = new ArrayList<>();
        List<Integer> num = new ArrayList<>();
        for (CustomerChartVO data : datas) {
            groups.add(data.getGroupByType());
            num.add(data.getNumber());
        }
        model.addAttribute("groups", JSONUtils.toJSONString(groups));
        model.addAttribute("num", JSONUtils.toJSONString(num));
        model.addAttribute("datas", datas);
        model.addAttribute("qo", qo);
        return "chart/customer_bar";
    }
    @RequestMapping("/pie")
    public String pie(Model model, CustomerChartQO qo) {
        List<CustomerChartVO> customerChartVOS = customerService.queryCustomerChart(qo);
        List<String> groups = new ArrayList<>();
       List<Map<String,Object>>data=new ArrayList<>();

        for (CustomerChartVO ch : customerChartVOS) {
            Map<String,Object> map=new HashMap<>();
            map.put("name",ch.getGroupByType());
            map.put("value",ch.getNumber());
            data.add(map);
            groups.add(ch.getGroupByType());
        }
        model.addAttribute("groups", JSONUtils.toJSONString(groups));
        model.addAttribute("datas", JSONUtils.toJSONString(data));
        model.addAttribute("qo", qo);
        return "chart/customer_pie";
    }
    @RequestMapping("/a")
    public String a(Model model, CustomerChartQO qo) {
        List<CustomerChartVO> customerChartVOS = customerService.queryCustomerChart(qo);
        List<String> groups = new ArrayList<>();
       List<Map<String,Object>>data=new ArrayList<>();

        for (CustomerChartVO ch : customerChartVOS) {
            Map<String,Object> map=new HashMap<>();
            map.put("name",ch.getGroupByType());
            map.put("value",ch.getNumber());
            data.add(map);
            groups.add(ch.getGroupByType());
        }
        model.addAttribute("groups", JSONUtils.toJSONString(groups));
        model.addAttribute("datas", JSONUtils.toJSONString(data));
        model.addAttribute("qo", qo);
        return "chart/customer_a";
    }

}
